package com.smec.remes.module.query.packing;

import android.app.ProgressDialog;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.google.gson.Gson;
import com.smec.moblie.R;
import com.smec.moblie.databinding.QueryPackingListBinding;
import com.smec.remes.base.SmecBaseFragment;
import com.smec.remes.bean.query.PackingList;
import com.smec.remes.module.query.SmecQueryPresenter;
import com.smec.remes.module.query.packing.adapter.SmecPackingAdapter;
import com.smec.remes.util.CommonUtils;
import com.smec.remes.util.GetFullUrl;
import com.smec.remes.util.GsonUtil;
import com.smec.remes.util.ScreenDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;

/**
 * Created by Administrator on 2018/1/4.
 */

public class SmecPackingFragment extends SmecBaseFragment<SmecQueryPresenter> {

    private QueryPackingListBinding queryPackingListBinding;
    public String _url="MntPackPortPS";
    private Gson gson = new Gson();
    private SmecPackingAdapter smecPackingAdapter;
    private ScreenDialog screenDialog;
    private ProgressDialog progressDialog;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        queryPackingListBinding=DataBindingUtil.inflate(inflater, R.layout.query_packing_list,container,false);
        smecPackingAdapter=new SmecPackingAdapter(getActivity());
        queryPackingListBinding.packingRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        queryPackingListBinding.packingRecyclerView.setAdapter(smecPackingAdapter);
        _Reset();
        _Query();
        return queryPackingListBinding.getRoot();
    }

    /**
     *  查询按钮
     * @return
     */
    public void _Query(){
        queryPackingListBinding.btnQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(TextUtils.isEmpty(queryPackingListBinding.edContrast.getText()) ||TextUtils.isEmpty(queryPackingListBinding.edLadder.getText()) ){
                    Toast.makeText(getActivity(),"请完善信息",Toast.LENGTH_SHORT).show();
                }else {
                    CommonUtils.hideImmManager(queryPackingListBinding.getRoot());
                    if(progressDialog == null){
                        progressDialog=new ProgressDialog(getActivity());
                    }
                    progressDialog.setMessage("正在查询.....");
                    progressDialog.show();
                    _getList();
                    queryPackingListBinding.packingRecyclerView.smoothScrollToPosition(0);
                }
            }
        });

    }

    /**
     *  调接口
     */
    public void _getList(){
        HashMap hashMap=new HashMap();
        hashMap.put("arg0",queryPackingListBinding.edContrast.getText().toString());
        hashMap.put("arg1",queryPackingListBinding.edLadder.getText().toString());
        String adress= GetFullUrl.getFullUrl(_url,hashMap);
        Log.w("URL", GetFullUrl.getFullUrl(_url,hashMap));
        OkHttpUtils.get().url(adress).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                progressDialog.dismiss();
            }

            @Override
            public void onResponse(String response, int id) {
                progressDialog.dismiss();
                PackingList[] packingLists=gson.fromJson(response.toString().trim(),PackingList[].class);
                smecPackingAdapter.setArrayList(Arrays.asList(packingLists));
            }
        });
    }

    /**
     *  重置按钮,是否过滤
     * @return
     */
    public void _Reset(){
        queryPackingListBinding.btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                queryPackingListBinding.edContrast.setText("");
                queryPackingListBinding.edLadder.setText("");
                queryPackingListBinding.edContrast.requestFocus();
                queryPackingListBinding.edLadder.clearFocus();
                queryPackingListBinding.invalidateAll();
            }
        });
        queryPackingListBinding.llListSeniorNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                screenDialog=new ScreenDialog(getActivity(),R.style.project_back);
                screenDialog.show();
                Button btnReset= (Button) screenDialog.findViewById(R.id.btn_reset_new);
                btnReset.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        screenDialog._clear();
                    }
                });
                Button btnQuery= (Button) screenDialog.findViewById(R.id.btn_query_new);
                btnQuery.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if(CommonUtils.notEmpty(smecPackingAdapter.getList())){
                            smecPackingAdapter._compare(screenDialog._PackList());
                        }else {
                            Toast.makeText(getActivity(),"请先查询装箱清单！",Toast.LENGTH_SHORT).show();
                        }
                        screenDialog.dismiss();
                    }
                });
            }
        });
    }

    @Override
    protected SmecQueryPresenter getPersenter() {
        return new SmecQueryPresenter(getActivity());
    }

    public static SmecPackingFragment newInstance() {
        Bundle args = new Bundle();
        SmecPackingFragment fragment = new SmecPackingFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        queryPackingListBinding.edLadder.setText("");
        queryPackingListBinding.edContrast.setText("");
    }
}
